<template>
  <div class="pay-index">
    <!-- 支付页面顶部导航栏 -->
    <div class="pay-nav">
      <van-nav-bar
      title="订单结算台"
      left-arrow
      @click-left="$router.go(-1)"
    />
    </div>
    <!-- 支付页面主体 -->
    <div class="pay-main">
      <!-- 订单地址 -->
      <div class="pay-address" v-if="addressList.length">
        <div class="address-icon"><van-icon name="logistics"/></div>
        <div class="address-info">
          <p class="mb-0 fontSize-14"><span>{{addressList[0].name}}</span><span class="ms-2">{{addressList[0].phone}}</span></p>
          <p class="mb-0 fontSize-14"><span>{{ longAddress }}</span></p>
        </div>
        <div class="address-edit" @click="onEdit"><van-icon name="arrow" /></div>
      </div>
      <!-- 无地址时提示 -->
      <div class="pay-address" v-else @click="onEdit">
        <div class="address-icon"><van-icon name="add" /></div>
        <div class="address-info">
          <p class="mb-0 fontSize-14"><span>请添加收货地址</span></p>
        </div>
        <div class="address-edit"><van-icon name="arrow" /></div>
      </div>
      <!-- 支付列表 -->
      <div class="pay-list">
        <van-card v-for="item in order.goodsList" :key="item.goods_id"
          :num="item.total_num"
          :price="item.goods_price"
          :desc="item.goods_no"
          :title="item.goods_name"
          :thumb="item.goods_image"
        />
        <div class="d-flex justify-content-end p-r_10 m-t_8 fontSize-16">
          共<span class="mx-2">{{order.orderTotalNum}}</span>件商品，合计:<span class="text-danger ms-2">￥{{order.orderPrice}}</span>
        </div>
        <van-divider />
      </div>
      <!-- 支付内容 -->
      <div class="pay-content d-flex flex-column">
        <!-- 订单总金额 -->
        <div class="d-flex justify-content-between align-items-center p-x_10 fontSize-16">
          <div>订单总金额:</div>
          <div class="text-danger">￥{{order.orderTotalPrice}}</div>
        </div>
        <!-- 优惠券 -->
        <div class="coupon_cell">
          <!-- 优惠券单元格 -->
            <van-coupon-cell
              :coupons="coupons"
              :chosen-coupon="chosenCoupon"
              @click="showList = true"
            />
        </div>
        <!-- 配送费用 -->
        <div class="d-flex justify-content-between align-items-center p-x_10 fontSize-16">
          <div>配送费用:</div>
          <div v-if="!selectAddress" @click="onEdit">请先选择配送地址</div>
          <div v-else class="text-danger">+￥0.00</div>
        </div>
        <van-divider />
        <!-- 支付方式 -->
        <div class="d-flex flex-column p-x_10 fontSize-16 pay-method">
          <div class="pay-method-title">支付方式</div>
          <div class="d-flex justify-content-between align-items-center pay-method-money">
            <div class="pay-method-icon"><van-icon name="balance-o" /></div>
            <div class="pay-method-text fontSize-14">余额支付（可用余额：<span>￥{{personal.balance}}</span>）</div>
            <div class="pay-method-check"><van-icon name="passed" /></div>
          </div>
        </div>
        <!-- 备注选项 -->
        <div class="remark">
        <van-field
            v-model="remark"
            rows="2"
            autosize
            label="备注(选填)"
            type="textarea"
            maxlength="50"
            placeholder="请输入备注"
            show-word-limit
          />
        </div>
      </div>
    </div>
    <!-- 底部支付按钮 -->
    <div class="pay-footer">
        <div class="fontSize-16">实付款：<span class="text-danger">￥{{order.orderPayPrice}}</span></div>
        <div><button type="button" class="btn btn-primary btn-block buy-btn-color" @click="placeOrder">提交订单</button></div>
    </div>
  </div>
</template>

<script>
import { getAddressList } from '@/api/address'
import { getOrderInfo, submitOrder } from '@/api/order'
export default {
  name: 'PayIndex',
  data () {
    return {
      addressList: [], // 地址列表
      showList: false,
      chosenCoupon: -1, // 选择的优惠券
      coupons: [], // 优惠券列表
      remark: '', // 备注信息
      order: {}, // 订单信息
      personal: {} // 个人信息
    }
  },
  computed: {
    selectAddress () {
      // 直接获取第一个默认地址
      return this.addressList[0] || {}
    },
    longAddress () {
      const region = this.selectAddress.region
      return region.province + region.city + region.region + this.selectAddress.detail
    },
    mode () {
      return this.$route.query.mode
    },
    cartIds () {
      return this.$route.query.cartIds
    },
    goodsId () {
      return this.$route.query.goodsId
    },
    goodsNum () {
      return this.$route.query.goodsNum
    },
    goodsSkuId () {
      return this.$route.query.goodsSkuId
    }
  },
  async created () {
    this.getAddressList()
    this.getOrderList()
  },
  methods: {
    onEdit () {
      this.$router.push('/addressList')
    },
    async getAddressList () {
      const { data: { list } } = await getAddressList()
      this.addressList = list
      // console.log(this.addressList)
    },
    async getOrderList () {
      if (this.mode === 'cart') {
        const { data: { order, personal } } = await getOrderInfo(this.mode, { cartIds: this.cartIds })
        this.order = order
        // console.log(this.order)

        this.personal = personal
      } else if (this.mode === 'buyNow') {
        const { data: { order, personal } } = await getOrderInfo(this.mode, { goodsId: this.goodsId, goodsNum: this.goodsNum, goodsSkuId: this.goodsSkuId })
        this.order = order
        this.personal = personal
      }
    },
    async placeOrder () {
      if (this.mode === 'cart') {
        await submitOrder(
          this.mode,
          {
            cartIds: this.cartIds,
            remark: this.remark
          }
        )
      } else if (this.mode === 'buyNow') {
        await submitOrder(
          this.mode,
          {
            goodsId: this.goodsId,
            goodsNum: this.goodsNum,
            goodsSkuId: this.goodsSkuId,
            remark: this.remark
          }
        )
      }
      this.$toast.success('提交成功')
      this.$router.replace('/myorder')
    }
  }
}
</script>

<style lang="less" scoped>
  .pay-index {
    display: flex;
    flex-direction: column;
    // 分隔线样式
    ::v-deep(.van-divider){
      margin: 10px 0;
    }
    .pay-nav{ //顶部导航栏样式
      width: 100%;
      height: 50px;
      position: fixed;
      z-index: 999;
      top: 0;
      border-bottom: 1.2px solid #E80505;
      ::v-deep(.van-nav-bar){
        height: 100%;
        line-height: 25px;
        .van-nav-bar__content{
          height: 50px;
        }
      }
    }
    .pay-main{ //支付页面主体样式
      margin: 50px 0 55px 0;
      display: flex;
      flex-direction: column;
      .pay-address{ //地址栏样式
        padding: 16px 10px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .address-icon {
          flex: 0;
          font-size: 16px;
        }
        .address-info{
          flex: 1;
          margin: 0 20px;
        }
        .address-edit{
          flex: 0;
          font-size: 16px;
        }
      }
      .pay-list{ //支付列表样式
        ::v-deep(.van-card){
          .van-card__content{
            padding: 4px 0;
            .van-card__title{
              font-size: 16px;
              margin-bottom:8px ;
            }
          }
          .van-card__bottom{
            .van-card__price{
              color: #E80505;
            }
          }
        }
      }
      .pay-content{ //支付内容样式
        .coupon_cell{ //优惠券单元格样式
          ::v-deep{
            .van-cell{
              padding: 10px;
              font-size: 16px;
              .van-cell__value{
                font-size:14px;
              }
              .van-icon{
                font-size: 14px;
              }
              &:focus,&:hover{
                background-color: #fff;
              }
            }
          }
        }
        .pay-method{ //支付方式样式
          .pay-method-icon{
            flex: 0;
            font-size: 26px;
            margin-right: 10px;
          }
          .pay-method-text{
            flex: 1;
            color: #333;
          }
          .pay-method-check{
            flex: 0;
            font-size: 20px;
            color: #E80505;
          }
        }
        .remark{ //备注选项样式
          ::v-deep(.van-cell){
            padding: 10px;
            .van-cell__title{
              font-size: 15px;
            }
          }
        }
      }
    }
    .pay-footer{ //底部支付按钮样式
      width: 100%;
      height: 55px;
      position: fixed;
      bottom: 0;
      padding: 8px 10px;
      z-index: 999;
      background-color: #fff;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-top: 1.2px solid #E80505;
      .btn{
        padding: 8px 16px;
        font-size: 14px;
        border-radius: 25px;
        border: none;
        color: #fff;
        cursor: pointer;
      }
    }
  }
</style>
